/*
Neighborhoods, Perceived Inequality, and Preferences for Redistribution:
Evidence from Barcelona
 
by Gerard Domènech-Arumí
December 2024
 
master do-file

*/
********************************************************************************
* prelims
********************************************************************************
{
* clear environment
clear all
set more off
cap log close

* reproducibility
set seed 123

* R 4.1.3

* main directory
global dir "PATH_TO_MAIN_DIRECTORY_HERE"
global dir "~/Dropbox/research/space inequality/jpube replication files"
cd "$dir"

* project directories
global prog "$dir/prog"
global orig "$dir/orig"
global data "$dir/data"
global figs "$dir/figs"
global tables "$dir/tables"

}
********************************************************************************
* data cleaning
********************************************************************************
{

** Catastro [CONFIDENTIAL]
* clean catastro data
do "$prog/clean_catastro.do"
* add census tract, neighborhood, and coordinates to Barcelona parcels
rscript using "$prog/catastro_match_census.R", rpath($dir)
* generate new variables from the Barcelona catastro
do "$prog/catastro_agglevel_des.do"

** ATC [CONFIDENTIAL]
rscript using "$prog/atc_itp.R", rpath($dir)

** Ajuntament de Barcelona/ AMB
* house prices
do "$prog/ajbcn_houseprice.do"
* metro, schools, and parks
do "$prog/bcn_metro_schools_parks_hosp.do"
* distance to metro stations, schools and parks -- R --
rscript using "$prog/dist_metro_schools_parks_hosp_tract.R", rpath($dir)
* identify closest public transit stations
do "$prog/distance_trans.do"

** INE
* clean 2011 census
do "$prog/census_2011.do"	
* opportunity atlas
do "$prog/ine_atlas_renta.do"
* INE-ECV national income distribution
do "$prog/ine_ecv.do"
* demographic information from municipal registry
do "$prog/ine_registry_ctract_bcn.do"

** Ministerio del Interior: 2015/19 election results for bcn
do "$prog/elections_bcn.do" 

** Ministerio de Fomento: rentals at csec level
do "$prog/mfomento_rentals.do"

}
********************************************************************************
* predict dwelling prices and compute inequality estimates
********************************************************************************
{

** predict dwelling prices
* put together a dataset with variables to predict dwelling prices
do "$prog/prepare_predict_price_bcn.do"
* estimate dwelling prices using random forest -- R -- 
rscript using "$prog/predict_price_bcn.R", rpath($dir)

** LNG
* compute LNG in BCN and other large Spanish cities -- R --
rscript using "$prog/ineq_lng.R", rpath($dir)
* combine intermediate datasets
do "$prog/lng_data_combine.do"
* generate summary measures of inequality at diff aggregations in BCN
do "$prog/inequality_bcn.do"

}
********************************************************************************
* clean survey
********************************************************************************
{

* clean survey data
do "$prog/survey_cleaning.do"

* match addresses from survey to catastro using fuzzy string matching -- R --
rscript using "$prog/survey_matchaddress.R"

* manually check and update results of matching
* match to an address for those unmatched or wrongly matched
* note: this step is not reproducible

* convert to stata format
do "$prog/address_match_final.do"

}
********************************************************************************
* generate analysis dataset
********************************************************************************
{

* calculate distances between respondents and new buildings -- R --
rscript using "$prog/neigh_shocks.R", rpath($dir)

* stata data building shocks csec
do "$prog/blng_shock_csec.do"

* generate analysis dataset
do "$prog/gen_analysis_dataset.do"

}
********************************************************************************
* analysis
********************************************************************************
{

* main analysis
do "$prog/analysis.do"

* generate maps -- R --
rscript using "$prog/maps_ineq.R", rpath($dir)

}
********************************************************************************
* closing
********************************************************************************
{

cap log close
clear

}
